package com.yogurt.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yogurt.edu.entity.Course;
import com.yogurt.edu.entity.Teacher;
import com.yogurt.edu.entity.vo.CourseInfoVo;
import com.yogurt.edu.entity.vo.CoursePublishVo;
import com.yogurt.edu.entity.vo.CourseQuery;
import com.yogurt.edu.service.CourseService;
import com.yogurt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yogurt
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //课程列表分页显示,带有查询条件
    @PostMapping("/pageQuery/{currentPage}/{row}")
    public Result pageQuery(@PathVariable Long currentPage, @PathVariable Long row,
                            @RequestBody(required = false) CourseQuery courseQuery){
        //创建分页对象
        Page<Course> page = new Page<>(currentPage,row);
        //构建查询条件
        QueryWrapper<Course> wrapper = new QueryWrapper();

        //获取所有的查询条件
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();

        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            wrapper.eq("teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        wrapper.orderByDesc("gmt_create");
        courseService.page(page, wrapper);

        long total = page.getTotal(); //总记录数
        List<Course> records = page.getRecords(); //每页的数据List集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加课程信息
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody CourseInfoVo courseInfoVo){
        try{
            String id = courseService.addCourse(courseInfoVo);
            return Result.ok().data("courseId",id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    //根据id查询课程信息和描述信息,进行数据回显
    @GetMapping("/findCourseById/{courseId}")
    public Result findCourseById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.findCourseById(courseId);
        return Result.ok().data("courseInfo",courseInfoVo);
    }

    //修改课程和描述信息
    @PutMapping("/editCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程id查询发布查询课程的基本信息(多表查询)
    @GetMapping("/getCourseInfo/{courseId}")
    public Result getCoursePublishInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishInfo(courseId);
        if (coursePublishVo == null){
            return Result.error();
        }
        return Result.ok().data("coursePublishVo",coursePublishVo);
    }

    //根据课程id修改课程状态,课程最终发布,将status的状态由未发布(Draft)改为已发布(Normal)
    @PutMapping("/publishCourse/{courseId}")
    public Result publishCourse(@PathVariable String courseId){
        Course course = new Course();
        course.setId(courseId);
        course.setStatus("Normal");
        boolean flag = courseService.updateById(course);
        if (!flag){
            return Result.error();
        }
        return Result.ok();
    }

    //根据课程id删除课程(删除小节 -> 章节 -> 描述 -> 课程本身)
    @DeleteMapping("/deleteById/{courseId}")
    public Result deleteById(@PathVariable String courseId){
        try{
            courseService.deleteById(courseId);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

}

